<?php

class model_aliProduct extends L {

    /**
     * 描述 ：分页的查询语句拼接
     * 作者 ：Zoro.Zhu
    **/
    public static function searchSql($params = array()){
        $sql = "SELECT
             `productId`, `createDate`, `data`, `pddId`,`state`,
             `account`,`errorData`, `manuName`
        FROM
            `ali_product`
        WHERE 1";
        if(empty($params['search'])){
            $sql .= " AND 1 <> 1";
        }
        $sql .= !empty($params['search']['productId']) ? " AND `productId` = '{$params['search']['productId']}' " : "";
        $sql .= !empty($params['search']['createDateStart']) ? " AND `createDate` >= '{$params['search']['createDateStart']}' " : "";
        $sql .= !empty($params['search']['createDateEnd']) ? " AND `createDate` <= '{$params['search']['createDateEnd']}' " : "";
        $sql .= !empty($params['search']['pddId']) ? " AND `pddId` = '{$params['search']['pddId']}' " : "";
        $sql .= !empty($params['search']['state']) ? " AND `state` = '{$params['search']['state']}' " : "";
        $sql .= !empty($params['search']['account']) ? " AND `account` = '{$params['search']['account']}' " : "";
        $sql .= !empty($params['search']['manuName']) ? " AND `account` = '{$params['search']['manuName']}' " : "";
        return $sql;
    }

    /**
     * 描述 ：执行修改数据的sql
     * 作者 ：Zoro.Zhu
    **/
    public static function updateSql($data = array()){
        $sql = "UPDATE `ali_product` 
        SET 
            `createDate` = '{$data['createDate']}',
            `data` = '{$data['data']}',
            `pddId` = '{$data['pddId']}'
        WHERE
            `productId` = '{$data['productId']}'";
        return L::sql($sql);
    }

    /**
     * 描述 ：执行插入数据的sql
     * 作者 ：Zoro.Zhu
    **/
    public static function insertSql($data = array()){
        $sql = "INSERT INTO `ali_product`(
             `createDate`, `data`, `pddId`
        ) VALUES (
             '{$data['createDate']}', '{$data['data']}', '{$data['pddId']}'
        )";
        return L::sql($sql);
    }

    /**
     * 描述：根据主键获取数据
     * 作者：Zoro.Zhu
    **/
    public static function getKeyInfo($key){
        if(is_array($key)){
            $key = join("','", $key);
        }
        $sql = "SELECT
             `productId`, `createDate`, `data`, `pddId`, `account`, `state`
        FROM
            `ali_product`
        WHERE 
            `productId` IN ('{$key}')";
        return L::sql($sql);
    }

    /**
     * 描述 : 获取产品图片信息
     * 参数 : {
     *      productId : O产品ID
     *      type : 类型 10 主图 20 详情图
     *      isPddUrl : 有没有pddUrl true有false无
     * }
     * 作者 : Zoro.Zhu
     **/
    public static function getImg($params){
        $sql = "SELECT
            `productId`, `vsPath`, `type`, `pddUrl`
        FROM
            `ali_product_img`
        WHERE
            `productId` = '{$params['productId']}'";
        !empty($params['type']) && $sql .= " AND `type` = '{$params['type']}'";
        isset($params['isPddUrl']) && $params['isPddUrl'] === false && $sql .= " AND `pddUrl` IS NULL";
        isset($params['isPddUrl']) && $params['isPddUrl'] === true && $sql .= " AND `pddUrl` IS NOT NULL";
        $sql .= " ORDER BY `vsPath` ASC";
        return L::sql($sql);
    }

    /**
     * 描述 : 获取产品图片信息
     * 参数 :
     *      $productId : 产品ID
     * 返回值 : {
     *      阿里产品数据包
     * }
     * 作者 : Zoro.Zhu
     **/
    public static function getProductInfo($productId){
        $sql = "SELECT
            `data` ,`pddId`
        FROM
            `ali_product` 
        WHERE
            `productId` = '{$productId}'";
        $info = L::sql($sql);
        $data = [];
        if(!empty($info[0])){
            $data = json_decode($info[0]['data'], true);
            $data['pddId'] = $info[0]['pddId'];
        }

        return $data;
    }

    /**
     * 描述 : 获取产品属性数据
     * 参数 :
     *      [属性名称,....]
     * 返回值 : {
     *      属性数据
     * }
     * 作者 : Zoro.Zhu
     **/
    public static function getProductAttr($attrs){
        $sql = "SELECT
            `attrVal`, `pddSpecId` , `id`
        FROM
            `ali_product_attr`
        WHERE `attrVal` IN ( '" . join("','", $attrs). "' )";
        return L::sql($sql);
    }

    /**
     * 描述 ：获取ali的sku信息
     * 作者 ：Zoro.Zhu
    **/
    public static function getSku($sku){
        $sql = "SELECT
            `productId`, `specId` 
        FROM
            `ali_product_detail` 
        WHERE
            `sku` = '{$sku}'";
        $info = L::sql($sql);
        return !empty($info[0]) ? $info[0] : [];
    }

}